﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TemplatesPrint.DBHelper;
using TemplatesPrint.Entitys;

namespace TemplatesPrint.Views
{
    public partial class FrmSysConfig : UIPage
    {
     
        public FrmSysConfig()
        {
            InitializeComponent();
        }

        private void FrmClientInfo_Initialize(object sender, EventArgs e)
        {
            uiPagination1.PageSize = 50;
            QryInfos(1, uiPagination1.PageSize, utxtQry.Text.Trim());
        }


       
        private void QryInfos(int pageIndex, int pageSize, string txt)
        {
            using (var db = SQLiteHelper.GetInstance())
            {
                int totalCount = 0;
                var tasks = db.Queryable<SchemeAmountEntity>()
                    .Where(x => x.JoiningScheme.Contains(txt))
                    .OrderBy(x => x.CreateTime)
                    .ToPageList(pageIndex, pageSize, ref totalCount);

                udgvJiaMengDianList.DataSource = tasks;
                uiPagination1.TotalCount = totalCount;
            };
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            QryInfos(pageIndex, count, utxtQry.Text.Trim());
        }

        private void usbtnQry_Click(object sender, EventArgs e)
        {
            QryInfos(1, uiPagination1.PageSize, utxtQry.Text.Trim());
        }

        private SchemeAmountEntity DgvSelected()
        {
            SchemeAmountEntity scheme = null;
            int rows = this.udgvJiaMengDianList.SelectedRows.Count;
            if (rows > 0)
            {
                var id = this.udgvJiaMengDianList.SelectedRows[0].Cells["Id"].Value.ToString();
                using (var db = SQLiteHelper.GetInstance())
                {

                    scheme = db.Queryable<SchemeAmountEntity>()
                        .Where(x => x.Id == int.Parse(id))
                        .First();
                };
            }
            else
            {
                ShowInfoDialog("请先选择需要操作的数据！");
            }
            return scheme;
        }

        private void tsbButton_Click(object sender, EventArgs e)
        {
            ToolStripButton tsb = (ToolStripButton)sender;
            switch (tsb.Name)
            {
                case "tsbCreate":
                    TsbCreate();
                    break;
                case "tsbDelete":
                    TsbDelete();
                    break;
                case "tsbEdit":
                    TsbEdit();
                    break;
            }
            QryInfos(1, uiPagination1.PageSize, utxtQry.Text.Trim());
        }

        void TsbCreate()
        {
            FrmCreateSys frmCreate = new FrmCreateSys();
            frmCreate.ShowDialog();
        }
        void TsbEdit()
        {
            var schemeEntity= DgvSelected();
            FrmCreateSys frmCreate = new FrmCreateSys(schemeEntity);
            frmCreate.ShowDialog();
        }

        void TsbDelete()
        {
            var schemeEntity = DgvSelected();
            DialogResult result = MessageBox.Show("是否确认删除此数据，删除后不可恢复?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                using (var db = SQLiteHelper.GetInstance())
                {
                    db.Deleteable(schemeEntity).ExecuteCommand();
                };
                MessageBox.Show("删除成功!");
            }
        }
    }
}
